<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 销售人员管理
                </el-breadcrumb-item>
            </el-breadcrumb>
            <div class="container">
                <div class="handle-box">
                    <el-select v-model="ser.org" class="handle-select mr10">
                        <el-option label="全部" :value="0"></el-option>
                        <el-option :key="it.value" :label="it.name" :value="it.value" v-for="it in orgs"></el-option>
                        <template #prefix>部门管理</template>
                    </el-select>
                    <el-input v-model="ser.key" placeholder="登录名" style="width:260px;" class="handle-input mr10">
                        <template #prepend>登录名</template>
                    </el-input>
                    <el-button type="primary" icon="el-icon-search" @click="searchClick">搜索</el-button>
                    <el-button @click="addClick">添加</el-button>
                </div>
                <el-table :data="datas" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                    <el-table-column prop="name" label="登录名"></el-table-column>
                    <el-table-column prop="trueName" label="真实姓名"></el-table-column>
                    <el-table-column prop="orgName" label="部门"></el-table-column>
                    <el-table-column prop="sex" label="性别"></el-table-column>
                    <el-table-column prop="isLock" label="是否锁定" v-if="bigScreen"
                        :formatter="boolformatter"></el-table-column>
                    <el-table-column prop="isAdmin" label="是否管理员" v-if="bigScreen"
                        :formatter="boolformatter"></el-table-column>
                    <el-table-column prop="createTime" label="创建时间" v-if="bigScreen"
                        :formatter="dateformatter"></el-table-column>
                    <el-table-column label="操作" width="180" align="center">
                        <template #default="scope">
                            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑
                            </el-button>
                            <el-button type="text" icon="el-icon-delete" class="red"
                                @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination">
                    <el-pagination background layout="total, prev, pager, next" :current-page="ser.page"
                        :page-size="ser.row" :total="total" @current-change="handlePageChange"></el-pagination>
                </div>
            </div>
        </div>
        <form-add ref="f_add" @search="search"></form-add>
    </div>
</template>
<script>
import FormAdd from './Add.vue'
export default {
    components: { FormAdd },
    data() {
        return {
            ser: {
                page: 1,
                row: 15,
                key: '',
                org: 0
            },
            datas: [],
            total: 0,
            orgs: [],
            bigScreen: window.innerWidth > 768,
        }
    },
    methods: {
        searchClick() {
            this.handlePageChange(1);
        },
        search() {
            this.$api.saler.search(this.ser).then(({ msg: { items, total } }) => {
                this.datas = items;
                this.total = total;
            })
        },
        handlePageChange(page) {
            this.ser.page = page;
            this.search();
        },
        handleEdit(row) {
            this.$refs.f_add.showadd(row);
        },
        handleDelete(row) {
            this.$confirm('是否确定要删除当前').then(() => {
                const { id } = row;
                this.$api.saler.remove(id).then(({ state, msg }) => {
                    this.$message[state ? 'success' : 'warning'](msg);
                    if (state) {
                        this.search();
                    }
                })
            }).catch(() => {
                console.log('取消了');
            })
        },
        addClick() {
            this.$refs.f_add.showadd();
        },
        boolformatter: (...arr) => ['否', '是'][+arr[2]],
        dateformatter: (...arr) => arr[2].replace('T', ' ').slice(0, 19)
    },
    mounted() {
        this.searchClick();
        this.$api.dictionary.search(3).then(({ msg }) => {
            this.orgs = msg;
        })
    }
}
</script>